package com.mandala.util;

import java.io.InputStream;
import java.util.Properties;


public class PropertiesUtil {

    public static Properties prop;

    static{
    	ini();
    }
    
    public static void ini() {
        prop = new Properties();
        try {
            InputStream is = getResourceAsStream("ssosystemconfig.config.properties");
            if (is != null) {
            	prop.load(is);
                is.close();
            }
        } catch (Exception e) {
            System.out.println("异常:" + e.getMessage());
        }
    }

    
    public static String getText(String key) {
        String value = "";
        try {
            if(prop == null){
                ini();
            }
            value = prop.getProperty(key);
            if (value == null) {
                return "";
            }
            return value.trim();
        } catch (Exception e) {
        	System.out.println("异常:" + e.getMessage());
            return value;
        }
    }
    

    public static int getInt(String key, int defaultPort) {
        try {
            if(prop == null){
                ini();
            }
            return Integer.parseInt(prop.getProperty(key));
        } catch (Exception e) {
        	System.out.println("异常:"+ e.getMessage());
            return defaultPort;
        }
    }
    public static InputStream getResourceAsStream(String resource) throws Exception {
        String stripped = resource.startsWith("/") ? resource.substring(1)
                : resource;

        InputStream stream = null;
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        if (classLoader != null) {
            stream = classLoader.getResourceAsStream(stripped);
        }
        if (stream == null) {
            stream = PropertiesUtil.class
                    .getResourceAsStream(resource);
        }
        if (stream == null) {
            stream = PropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(stripped);
        }
        if (stream == null) {
            throw new Exception(resource + " not found");
        }
        return stream;
    }
}
